package com.glsc.ngateway.platform.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.glsc.ngateway.common.api.common.enums.DictConstant;
import com.glsc.ngateway.common.api.common.utils.PageUtil;
import com.glsc.ngateway.common.base.domain.mysql.gateway.ldap.DepartmentVisitSystem;
import com.glsc.ngateway.common.base.domain.mysql.gateway.system.SystemMain;
import com.glsc.ngateway.common.base.exception.PlatformException;
import com.glsc.ngateway.common.base.repo.mysql.gateway.ldap.LdapDepartmentSystemRepo;
import com.glsc.ngateway.common.base.repo.mysql.gateway.platform.it.ItApplicationDetailRepo;
import com.glsc.ngateway.common.base.repo.mysql.gateway.platform.it.ItOperationRepo;
import com.glsc.ngateway.common.base.repo.mysql.gateway.platform.it.ItVirtualDeviceRepo;
import com.glsc.ngateway.platform.service.it.SequenceService;
import com.glsc.ngateway.platform.service.it.ServerService;
import com.glsc.ngateway.platform.utils.SpecificationUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

@Service
public class LdapDepartmentVisitService {

    Logger logger = LoggerFactory.getLogger(LdapDepartmentVisitService.class);
    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Autowired
    private SequenceService sequenceService;
    @Autowired
    private UserService userService;
    @Autowired
    private ItVirtualDeviceRepo itVirtualDeviceRepo;
    @Autowired
    private ItOperationRepo itOperationRepo;
    @Autowired
    private ItApplicationDetailRepo itApplicationDetailRepo;

    @Autowired
    private ServerService serverService;

    @Autowired
    private MailService mailService;

    @Resource
    private LdapDepartmentSystemRepo ldapDepartmentSystemRepo;


    @Transactional
    public void addOrUpdate(DepartmentVisitSystem systemMember) {
//        SystemVendorLogInfo log = new SystemVendorLogInfo();
//        if (StrUtil.isNotEmpty(systemMember.getVendorCode())){
//            //修改
//            log.setLogFlag(DictConstant.UPDATE);
//        } else {
//            //新增
//            log.setLogFlag(DictConstant.ADD);
//        }
//        BeanUtil.copyProperties(systemMember,log);
//        systemMember.setDeleted(DictConstant.NO_DELETE);
        DepartmentVisitSystem db = ldapDepartmentSystemRepo.findOneByDepartmentAndComptitle(systemMember.getDepartment(),systemMember.getComptitle());
//        if (ObjectUtil.isEmpty(db)){
//            db = ldapDepartmentSystemRepo.findOneByDepartmentAndComptitleIsNull(systemMember.getDepartment());
//        }
        //更新库里的数据，没有再新增
        if (ObjectUtil.isNotEmpty(db) && systemMember.getId() == null){
            BeanUtil.copyProperties(systemMember,db,"id");
            ldapDepartmentSystemRepo.save(db);
        } else {
            ldapDepartmentSystemRepo.save(systemMember);
        }

//        systemVendorLogInfoRepository.save(log);
    }

    public void delete(Integer id) {
        Optional<DepartmentVisitSystem> optional = ldapDepartmentSystemRepo.findById(id);
        if (optional.isPresent()) {
            DepartmentVisitSystem bean = optional.get();
//            bean.setDeleted(DictConstant.DELETED);

            ldapDepartmentSystemRepo.deleteById(id);
        } else {
            throw PlatformException.errorWithOutMail("删除记录不存在");
        }
    }

//    public List<DepartmentVisitSystem> search(DepartmentVisitSystem params) {
//        params.setDeleted(DictConstant.NO_DELETE);
//        List<DepartmentVisitSystem> result = ldapDepartmentSystemRepo.searchByParam(params);
//        return result;
//    }

//    public List<String> querySystemCodeByMemberAccount(String account) {
//        List<SystemMain> systemMains = ldapDepartmentSystemRepo.querySystemCodeByMemberAccount(account);
//        if (CollectionUtil.isNotEmpty(systemMains)) {
//            return systemMains.stream().map(SystemMain::getSystemClassNo).collect(Collectors.toList());
//        } else {
//            return null;
//        }
//    }


    public Page<DepartmentVisitSystem> page(int pageNo, int pageSize, Map<String, Object> param) {
        Sort.Direction direction = Sort.Direction.DESC;
        String sort = "id";

        pageNo = PageUtil.initPageNo(pageNo);
        pageSize = PageUtil.initPageSize(pageSize);

        Pageable pageable = PageRequest.of(pageNo - 1, pageSize, direction, sort);
//        param.put("EQ_deleted", 0);
        Page<DepartmentVisitSystem> page = ldapDepartmentSystemRepo.findAll(SpecificationUtil.buildSpecification(param, DepartmentVisitSystem.class), pageable);

        return page;
    }
}
